﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Logica.Modulos.ClasesLogica;
using Modelo.Modulos.ClasesModelado;

namespace ProyectoFinal
{   
    public partial class frmAdminPerfil : Form
    {
        private int idPerfilPermiso;
        public int IdPerfilPermiso
        {
            get { return idPerfilPermiso; }
            set { idPerfilPermiso = value; }
        }
        string Accion_Form="";
        public frmAdminPerfil()
        {
            InitializeComponent();
        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            try
            {
                frmInfoPerfil infoPerfil = new frmInfoPerfil();
                infoPerfil.Accion = "Agregar";
                infoPerfil.ShowDialog();
                Accion_Form = "Cargar";
                CargarGrid(Accion_Form);

            }
            catch (Exception exc)
            {
                MessageBox.Show(exc.Message, "Error del Sistema", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            try
            {
                frmInfoPerfil infoPerfil = new frmInfoPerfil();
                infoPerfil.Accion = "Editar";
                int Idindice = dataGridView1.SelectedRows[0].Index;
                infoPerfil.IdPerfil = Convert.ToInt32(dataGridView1[0, Idindice].Value);
                infoPerfil.ShowDialog();
                Accion_Form = "Cargar";
                CargarGrid(Accion_Form);
            }
            catch (Exception exc)
            {

                MessageBox.Show(exc.Message, "Error del Sistema", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            
        }

        private void btnModificar_Click(object sender, EventArgs e)
        {
            frmInfoPerfil infoPerfil = new frmInfoPerfil();
            infoPerfil.Accion = "Ver";
            int Idindice = dataGridView1.SelectedRows[0].Index;
            infoPerfil.IdPerfil = Convert.ToInt32(dataGridView1[0, Idindice].Value);
            infoPerfil.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void frmAdminPerfil_Load(object sender, EventArgs e)
        {
            CargarPermisos();
            Accion_Form = "Cargar";
            CargarGrid(Accion_Form);
        }

        public void CargarGrid(string Accion_Form)
        {
            if (Accion_Form == "Cargar") 
            {
                dataGridView1.Rows.Clear();
                Perfil_PermisoLog logica = new Perfil_PermisoLog();
                List<PerfilesMod> listaPerfiles = logica.ConsultarPerfiles();

                for (int i = 0; i < listaPerfiles.Count; i++)
                {
                    dataGridView1.Rows.Add();
                    dataGridView1.Rows[i].Cells[0].Value = listaPerfiles[i].IdPerfil;
                    dataGridView1.Rows[i].Cells[1].Value = listaPerfiles[i].NombrePerfil;
                    dataGridView1.Rows[i].Cells[2].Value = listaPerfiles[i].DescripcionPerfil;
                    
                }
            }

            else
            {
                if (Accion_Form == "Eliminar")
                {
                    Perfil_PermisoLog logica = new Perfil_PermisoLog();
                    List<PerfilesMod> listaPerfiles = logica.ConsultarPerfiles();

                    DataGridViewSelectedRowCollection listaGrid = dataGridView1.SelectedRows;
                    for (int i = listaGrid.Count - 1; i >= 0; i--)
                    {
                        dataGridView1.Rows.RemoveAt(listaGrid[i].Index);
                    }
                }

                else
                {


                }

            }

        }

        private void btnElimar_Click(object sender, EventArgs e)
        {
            try
            {
                Accion_Form = "Eliminar";
                int Idindice = dataGridView1.SelectedRows[0].Index;
                int Ideliminar = Convert.ToInt32(dataGridView1[0, Idindice].Value);

                PerfilesMod objMod = new PerfilesMod();
                objMod.IdPerfil = Ideliminar;


                Perfil_PermisoLog Logica = new Perfil_PermisoLog();
                Logica.EliminarPerfil(objMod);

                CargarGrid(Accion_Form);
            }
            catch (Exception exc)
            {

                MessageBox.Show(exc.Message, "Error del Sistema", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        public void CargarPermisos()
        {
            btnAgregar.Visible = false;
            btnEditar.Visible = false;
            btnElimar.Visible = false;
            btnVer.Visible = false;
            Perfil_PermisoLog Logica = new Perfil_PermisoLog();
            PerfilesMod objMod = new PerfilesMod();
            objMod.IdPerfil = idPerfilPermiso;
            List<PerfilesMod> listaPermisos_Perfil = Logica.ConsultarPermisoIdPerfil(objMod);



            for (int i = 0; i < listaPermisos_Perfil.Count; i++)
            {
                if (listaPermisos_Perfil[i].NombrePermiso == "Crear Perfil")
                {
                    btnAgregar.Visible = true;
                }
                else
                {
                    if (listaPermisos_Perfil[i].NombrePermiso == "Modificar Perfil")
                    {
                        btnEditar.Visible = true;
                        
                    }
                    else
                    {
                        if (listaPermisos_Perfil[i].NombrePermiso == "Eliminar Perfil")
                        {
                            btnElimar.Visible = true;
                        }
                        else
                        {
                            if (listaPermisos_Perfil[i].NombrePermiso == "Ver Perfil")
                            {
                                btnVer.Visible = true;
                            }
                        }
                    }

                }
            }
        }




    }
}
